#! /bin/bash

COUNT=5
SRC=$1
PREFIX=$2

usage()
{
    echo "Usage: $0 src dst"
    exit 1
}

(( $# == 2 )) || usage

i=$COUNT

if [[ ! -e "$SRC" ]]; then
    echo "SRC DIR [$SRC] NOT EXIST. ABORTING ..."
    exit 1
fi

echo 'REMOVING THE OLDEST ...'
rm -rf "$PREFIX.$i"
(( i -- ))

echo 'TOTATING BACKUP ...'
for ((; i > 0; i--)); do
    [[ -e "$PREFIX.$i" ]] &&  mv "$PREFIX.$i" "$PREFIX".$(( i + 1 ))
done

[[ -e "$PREFIX.0" ]] && cp -al $PREFIX.0 $PREFIX.1

echo 'INCREMENTAL RSYNC ...'
rsync -a --delete $SRC/ $PREFIX.0

echo 'DONE.'
